package com.agileboot.domain.airport.report.query;

import cn.hutool.core.util.StrUtil;
import com.agileboot.common.core.page.AbstractPageQuery;
import com.agileboot.domain.airport.report.db.SirReportEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Arrays;
import java.util.Date;

/**
 * 安全信息查询对象
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class ReportQuery extends AbstractPageQuery<SirReportEntity> {

    private String title;
    private String reportType;
    private String terminalType;
    private String infoSource; // 新增
    private Long dutyDeptId; // 新增
    private Long acceptDeptId; // 新增

    private String status; // 新增
    private Long discoverer; // 新增
    private Date discoveryDate; // 新增
    private Long reportId; // 新增
    private String discoverUserName;
    private Long contactPerson;

    @Override
    public QueryWrapper<SirReportEntity> addQueryCondition() {
        QueryWrapper<SirReportEntity> queryWrapper = new QueryWrapper<SirReportEntity>()
                .like(StrUtil.isNotEmpty(title), "title", title)
                .eq(StrUtil.isNotEmpty(reportType), "report_type", reportType)
                .eq(StrUtil.isNotEmpty(terminalType), "terminal_type", terminalType)
                .eq(StrUtil.isNotEmpty(infoSource), "info_source", infoSource) // 新增
                .eq(dutyDeptId != null, "duty_dept_id", dutyDeptId) // 新增
                .eq(acceptDeptId != null, "accept_dept_id", acceptDeptId) // 新增

                .eq(discoverer != null, "discoverer", discoverer) // 新增
                .eq(discoveryDate != null, "discovery_date", discoveryDate)
                .eq(contactPerson != null, "contact_person", contactPerson)
                .eq(reportId != null, "report_id", reportId); // 新增


        // 默认按创建时间排序
        if (StrUtil.isEmpty(this.getOrderColumn())) {
            this.setOrderColumn("discovery_date");
        }

        // 处理 status 字段
        if (StrUtil.isNotEmpty(status)) {
            String[] statusArray = status.split(",");
            if (statusArray.length == 1) {
                queryWrapper.eq("status", statusArray[0]);
            } else {
                queryWrapper.in("status", Arrays.asList(statusArray));
            }
        }

        this.setTimeRangeColumn("discovery_date");

        return queryWrapper;
    }
}
